1791D - Distinct Split - CodeForces Solution


brute force greedy strings

Please click on ads to support us..

C++ Code:

/**
 * author: Ashif Akhtar
 **/
#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

#define sync ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

#define pb push_back
#define mp make_pair
#define um unordered_map
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
const ll mod = 1000000007;
#define endl "\n"
#define in(x , n) for (auto &i : x)cin >> i;
#define out(x , n) for (int i = 0; i < n; i++)cout << x[i] << " \n"[i == n - 1];
#define pi 3.14159265358979323846

// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>pb;
#define fo find_by_order
#define ok order_of_key
#define ins insert

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }

int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

void solve()
{
	int n;
	string s;
	cin >> n >> s;
	map<char, int>m1, m2;
	for (int i = 0; i < n; ++i)
	{
		m1[s[i]]++;
	}
	int ans1 = m1.size();
	int ans2 = 0;
	int Ans = ans1;
	for (int i = 0; i < n; ++i)
	{
		m1[s[i]]--;
		if (m1[s[i]] == 0)ans1--;
		m2[s[i]]++;
		ans2 = m2.size();
		Ans = max(ans1 + ans2, Ans);
	}
	cout << Ans << endl;
}

signed main()
{
	sync;
	int t = 1;
	cin >> t;
	for (int i = 1; i <= t; ++i)
	{
		// cout << "Case #" << i << ": ";
		solve();
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number